package com.gelian.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.gelian.library.model.GlDevice;

public class IntentManager {


    public static void goToDeviceActivity(Context context, GlDevice device) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(GlDevice.class.getSimpleName(), device);
        openActivity(context, DeviceActivity.class, bundle, false);
    }


    private static void openActivity(Context context
            , Class<? extends Activity> clazz
            , boolean isFinish
    ) {
        openActivity(context, clazz, null, isFinish);
    }

    private static void openActivity(Context context
            , Class<? extends Activity> activityClazz
            , Bundle bundle
            , boolean isFinish
    ) {
        try {
            Intent intent = new Intent(context, activityClazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            context.startActivity(intent);

            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                if (isFinish) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
